package com.feisuanyz.service.impl;

import com.feisuanyz.dto.OperationLogDTO;
import com.feisuanyz.entity.OperationLog;
import com.feisuanyz.enums.OperationType;
import com.feisuanyz.query.OperationLogQuery;
import com.feisuanyz.repository.EventManagementRepository;
import com.feisuanyz.repository.OperationLogRepository;
import com.feisuanyz.repository.TicketManagementRepository;
import com.feisuanyz.rest.RestResult;
import com.feisuanyz.service.OperationLogService;
import java.sql.Timestamp;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class OperationLogServiceImpl implements OperationLogService {

    @Autowired
    private OperationLogRepository operationLogRepository;

    @Autowired
    private EventManagementRepository eventManagementRepository;

    @Autowired
    private TicketManagementRepository ticketManagementRepository;

    @Autowired
    private ModelMapper modelMapper;

    @Override
    @Transactional
    public RestResult<Void> logEventOperation(Integer eventId, String operationTypeStr, String details, Integer createBy) {
        Optional<OperationType> operationTypeOpt = OperationType.valueOf(operationTypeStr.toUpperCase());
        if (!operationTypeOpt.isPresent()) {
            return RestResult.fail("000001", "操作类型无效");
        }
        OperationType operationType = operationTypeOpt.get();

        if (!eventManagementRepository.existsById(eventId)) {
            return RestResult.fail("000001", "事件ID不存在");
        }

        OperationLog operationLog = new OperationLog(null, eventId, null, operationType, details, createBy, new Timestamp(System.currentTimeMillis()));
        operationLogRepository.save(operationLog);
        return RestResult.success();
    }

    @Override
    @Transactional
    public RestResult<Void> logTicketOperation(Integer ticketId, String operationTypeStr, String details, Integer createBy) {
        Optional<OperationType> operationTypeOpt = OperationType.valueOf(operationTypeStr.toUpperCase());
        if (!operationTypeOpt.isPresent()) {
            return RestResult.fail("000001", "操作类型无效");
        }
        OperationType operationType = operationTypeOpt.get();

        if (!ticketManagementRepository.existsById(ticketId)) {
            return RestResult.fail("000001", "票务ID不存在");
        }

        OperationLog operationLog = new OperationLog(null, null, ticketId, operationType, details, createBy, new Timestamp(System.currentTimeMillis()));
        operationLogRepository.save(operationLog);
        return RestResult.success();
    }

    @Override
    public RestResult<List<OperationLogDTO>> getEventOperationLogs(OperationLogQuery query) {
        Integer eventId = query.getEventId();
        if (eventId != null) {
            List<OperationLog> logs = operationLogRepository.findByEventId(eventId);
            if (logs.isEmpty()) {
                return RestResult.fail("000001", "未找到相关日志");
            }
            List<OperationLogDTO> logDTOs = logs.stream()
                    .map(log -> modelMapper.map(log, OperationLogDTO.class))
                    .collect(Collectors.toList());
            return RestResult.success(logDTOs);
        }
        return RestResult.fail("000001", "事件ID未提供");
    }

    @Override
    public RestResult<List<OperationLogDTO>> getTicketOperationLogs(OperationLogQuery query) {
        Integer ticketId = query.getTicketId();
        if (ticketId != null) {
            List<OperationLog> logs = operationLogRepository.findByTicketId(ticketId);
            if (logs.isEmpty()) {
                return RestResult.fail("000001", "未找到相关日志");
            }
            List<OperationLogDTO> logDTOs = logs.stream()
                    .map(log -> modelMapper.map(log, OperationLogDTO.class))
                    .collect(Collectors.toList());
            return RestResult.success(logDTOs);
        }
        return RestResult.fail("000001", "票务ID未提供");
    }
}